package com.superbytecode.cloud.example.albb.service;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * All rights Reserved, Designed By www.super-master.com
 *
 * @ProjectName: beacon-cloud
 * @Package: com.superbytecode.cloud.example.albb.service
 * @ClassName: TestSentinelService
 * @Description: []
 * <p> </p>
 * @Author: [Lance Ting]
 * @Date: 2024/11/4 17:11
 * @Version: V1.0
 * @Copyright: 2024 www.super-master.com Inc. All rights reserved.
 * TODO: 注意,本文件Lance Ting所作,如果转载或使用请标明具体出处!
 **/
@Service
public class TestSentinelService {

    @Value("${service-url.service-provider}")
    private String serverURL;

    @Resource
    private RestTemplate restTemplate;

    public HashMap<String, Object> testRemoteCall(Map<String, Object> params) {
        HttpEntity<?> request = obtainHttpEntity(params);
        return restTemplate.postForObject(serverURL + "/sample/server", request, HashMap.class);
    }

    private HttpEntity<?> obtainHttpEntity(Map<String, Object> params) {
        // 设置请求头，请求类型为json
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        //  设置请求参数
        //用HttpEntity封装整个请求报文
        return new HttpEntity<>(params, headers);
    }
}